home *** CD-ROM | disk | FTP | other *** search
- /* ungetc.c --- p 472 */
- #include <stdio.h>
- #include <ctype.h> /* For the macro isdigit() */
- main()
- {
- int intval = 0, c;
- char buff[81];
- /* Ask user to type in an integer */
- printf("Enter an integer followed by some other characters:");
- while ( (c = getchar()) != EOF && isdigit(c) )
- intval = 10*intval + c - 48; /* 0 is ASCII 48 */
- /* Push back the first non-digit read from stdin */
- if (c != EOF) ungetc(c, stdin);
- /* Print message to user */
- printf("Integer you entered = %d.\n Rest of the string beginning "
- "at the first non-integer in buffer: %s\n",
- intval, gets(buff));
- }